<?php
class Zendvn_Model_Table_Event extends Zend_Db_Table{
	protected $_name = "wp_event";
	protected $_primary = "id"; 
	protected $_arrParam = array();
	
	public $_searchFields = array('name'=>'- Name field','code'=>'- Code field');
	
	protected $_listSQL;
	public function statusItem($arrParam = null,$options = null){
		if($options == null){
			$status = ($arrParam['s'] == 'active' )? 'inactive':'active';				
			$data = array('status'=>$status);
			$where = 'id = ' . (int)$arrParam['id'];
			$this->update($data, $where);	
		}
		
		if($options['task'] == 'multi'){
			
			$status = $arrParam['s'];				
			if(count($arrParam['cid'])>0){
				if($status == 'active'){
					$ids = implode(',', $arrParam['cid']);
					$where = 'id IN (' . $ids . ')';
					$data = array('status'=>$status);
					$this->update($data, $where);
				}else{
					foreach ($arrParam['cid'] as $val){						
						$where = 'id = ' . $val;
						$data = array('status'=>$status);
						$this->update($data, $where);						
					}
				}
			}
		}
	}
	
	public function deleteItem($arrParam = null,$options = null){
		$model = new Zendvn_Model_DeleteRecode();
		$model->newsItem($arrParam,$options,$this,true);
		
		$tblItem = new Zendvn_Model_Table_LinkContent();
		$where = 'fk_news_item = ' . (int)$arrParam['id'];
		$tblItem->delete($where);
	}
	public function getItem($arrParam = null,$options = null){

            if($options == null){
                $acl= new Zendvn_Acl();

                $info = new Zendvn_System_Info();
                $idUser = $info->getMemberInfo('id');

                $where = 'id = ' . (int)$arrParam['id'];
                if(!$acl->isAdminAccess()){
                    $where.=' AND created_by='.$idUser;
                }
                $result = $this->fetchRow($where)->toArray();

            }
            if($options['task'] == 'info'){
			$db = $this->getAdapter();
			$select = $db->select()
						->from('news_item AS i')						
						->where('i.id = ?',$arrParam['id'],1)
						->joinLeft('users AS u1','u1.id = i.created_by', array ('u1.email AS created_by'))
						->joinLeft('users AS u2','u2.id = i.modified_by',array ('u2.email AS modified_by'));			
			$result = $db->fetchRow($select);

		}
        if(!empty($result['cat_id'])){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from('news_category AS c',array('name'))
                         ->where('c.id IN(?)',json_decode($result['cat_id'],true));
            $category = $db->fetchCol($select);
            $result['occupation'] = $category;
        }
        if($options['task'] == 'user'){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from('users AS c',array('email'))
                         ->where(
                            'CONCAT_WS(" ",first_name,last_name) = ?',
                            trim($arrParam['name'])
                         );
            $result = $db->fetchOne($select);
        }
		return $result;
	}
	

	
	public function totalItem($arrParam = null,$options = null){
		
		if($options == null){
			$db = $this->getAdapter();
			$select = $this->_listSQL->reset(Zend_Db_Select::COLUMNS);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_COUNT);
			$select = $this->_listSQL->reset(Zend_Db_Select::LIMIT_OFFSET);
			$select = $this->_listSQL->reset(Zend_Db_Select::ORDER);
			$select = $this->_listSQL->reset(Zend_Db_Select::GROUP);
			$select->columns('COUNT(i.id) AS total');	
			$result = $db->fetchOne($select);
		}
		return $result;
		
	}
	
	public function saveItem($arrParam = null,$options = null){
		
		$function  = Zendvn_System_Function::call($options['task']);
		return $this->$function($arrParam);
	}
	
	public function adminAdd($arrParam = null){
        $info = new Zendvn_System_Info();
        $idUser = $info->getMemberInfo('id');

		$row = $this->fetchNew();
		$row->title			= $arrParam['title'];
		$row->image			= @$arrParam['image'];
		$row->type			= $arrParam['type'];
		$row->bg_color		= $arrParam['bg_color'];
		$row->text_color	= $arrParam['text_color'];
		$row->description	= $arrParam['description'];
		$row->address		= $arrParam['address'];
		$row->location		= $arrParam['location'];
		$row->date		    = $arrParam['date'];
		$row->time		    = $arrParam['time'];
		$row->full_name		= $arrParam['full_name'];
		$row->created		= date('Y-m-d H:i:s');
		$row->created_by	= $idUser;
		$row->wp_id	        = $arrParam['wp_id'];
		$row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
		
		$row->save();

        return $row->id;
			
	}
	
	public function adminEdit($arrParam = null){
        $info = new Zendvn_System_Info();
        $idUser = $info->getMemberInfo('id');
		$where  			= 'id = ' . (int)$arrParam['id'];
		$row 				= $this->fetchRow($where);

        $row->title			= $arrParam['title'];
        $row->image			= $arrParam['image'];
        $row->type			= $arrParam['b_type'];
        $row->bg_color		= $arrParam['bg_color'];
        $row->text_color	= $arrParam['text_color'];
        $row->description	= $arrParam['description'];
        $row->address		= $arrParam['address'];
        $row->location		= $arrParam['location'];
        $row->date		    = date('Y-m-d',strtotime($arrParam['date']));
        $row->time		    = $arrParam['time'];
        $row->full_name		= $arrParam['full_name'];
        $row->modified_by	= $idUser;
        $row->status		= ($arrParam['status']=='') ? 'inactive':$arrParam['status'];
        $row->save();
        return (int)$arrParam['id'];
	}

    public function listItem($arrParam = null,$options = null){
        if($options == null){
            $info = new Zendvn_System_Info();
            $idUser = $info->getMemberInfo('id');

            $db = $this->getAdapter();
            $select = $db->select()
                         ->from(array('i' => $this->_name));
            $arrFilter = $arrParam['arrFilter'];

            if(!empty($arrFilter['col'])){
                $select->order($arrFilter['col'] . ' ' . $arrFilter['order']);
            }

            if(!empty($arrFilter['status']) && $arrFilter['status'] != 'none'){
                $select->where('i.status = ?',$arrFilter['status']);
            }

            if(!empty($arrFilter['feature']) && $arrFilter['feature'] != 'none'){
                $select->where('i.feature = ?',$arrFilter['feature']);
            }

            if(!empty($arrFilter['keyword']) && !empty($arrFilter['searchField'])){
                $keyword = '%' . $arrFilter['keyword'] . '%';
                $select->where($arrFilter['searchField'] . ' LIKE ? ', $keyword);
            }
            $acl= new Zendvn_Acl();
            if(!$acl->isAdminAccess()){
                $select->where('created_by = ?',$idUser);
            }
            $this->_listSQL = $select;
            $panigator = $arrParam['panigator'];
            $select->limitPage($panigator['page'], $panigator['rowCount']);

            $result = $db->fetchAll($select);
        }

        if($options['task'] == 'feature-service'){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from(array('i' => $this->_name), array('id', 'name', 'description', 'picture'))
                         ->where('i.status = "active" AND i.feature = "active"')
                         ->order('i.id ASC')
                         ->limitPage(0, 5);
            $result = $db->fetchAll($select);
        }

        if($options['task'] == 'right-service'){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from(array('i' => $this->_name), array('id', 'name', 'description', 'picture'))
                         ->where('i.status = "active"')
                         ->order('rand()')
                         ->limitPage(0, 5);
            $result = $db->fetchAll($select);
        }

        if($options['task'] == 'list-item'){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from(array('i' => $this->_name))
                         ->where('i.status = "active"')
                         ->order('i.id DESC');
            $this->_listSQL = $select;
            $panigator = $arrParam['panigator'];
            $select->limitPage($panigator['page'], $panigator['rowCount']);
            $result = $db->fetchAll($select);
        }

        if($options['task'] == 'search-item'){
            $db = $this->getAdapter();
            $select = $db->select()
                         ->from(array('i' => $this->_name))
                         ->columns(new Zend_Db_Expr("'service' AS data"))
                         ->where('i.name like "%' . $arrParam['keyword'] . '%"')
                         ->orWhere('i.content like "%' . $arrParam['keyword'] . '%"')
                         ->order('i.id DESC');
            $result = $db->fetchAll($select);
        }
        return $result;
    }
}